java从POM文件获取artifactId出错
为了获取项目中的artifactId,我使用以下代码
public static void main(String[] args) {
Properties prop = new Properties();
InputStream in = null;
try {
String fileName = "application.properties";
in = ConfigServiceApplication.class.getClassLoader().getResourceAsStream(fileName);
prop.load(in);
SpringApplication.run(ConfigServiceApplication.class, args);
System.out.println(prop.getProperty("prop.name");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我的申请书。属性包含以下内容:
prop.name=${project.artifactId}
如果一切正常运行,预期输出为:
config-service //this is my artifactId
但是当我运行上面的代码时得到的输出是
${project.artifactId}
我引用了这些链接来获取信息:Retrieve version from maven pom.xml in code, http://www.mkyong.com/java/java-properties-file-examples/
有人能更正我的代码以正确获取artifactId吗
编辑: Pom文件:
<project>
...
<artifactId>config-service</artifactId>
...
<properties>
<name>${project.artifactId}</name>
</properties>
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
...
</project>
# 1 楼答案
我认为目录结构或运行代码的方式有问题,因为配置看起来不错
见下面的小例子
波姆。xml
src/main/java/sub/optimal/mvnproperties/main。爪哇
src/main/resources/application。属性
现在编译并执行它
或者
在这两种情况下,属性的输出都是相同的